package com.wie.staffhubpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wie.staffhubpro.constant.MessageConstant;
import com.wie.staffhubpro.domain.dto.AttendanceDTO;
import com.wie.staffhubpro.domain.dto.CheckDTO;
import com.wie.staffhubpro.domain.entity.Attendance;
import com.wie.staffhubpro.domain.entity.Emp;
import com.wie.staffhubpro.domain.vo.AttdDeptVO;
import com.wie.staffhubpro.exception.DataErrorException;
import com.wie.staffhubpro.exception.EmployeeNotExistException;
import com.wie.staffhubpro.mapper.EmpMapper;
import com.wie.staffhubpro.service.AttendanceService;
import com.wie.staffhubpro.mapper.AttendanceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
* @author WiedersehenM
* @description 针对表【attendance】的数据库操作Service实现
* @createDate 2025-06-30 17:57:29
*/
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
    implements AttendanceService{

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private EmpMapper empMapper;

    /**
     * 查看今日考勤
     */
    public Attendance getToday(String empId) {
        LocalDate date = LocalDate.now();
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("att_date", date)
                .eq("emp_id", empId);
        List<Attendance> attendanceList = attendanceMapper.selectList(queryWrapper);
        if (attendanceList.isEmpty()) {
            return null;
        }
        return attendanceList.get(0);
    }

    /**
     * 查看个人历史考勤
     */
    public List<Attendance> getHistory(AttendanceDTO attendanceDTO) {
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", attendanceDTO.getEmpId())
                .eq(attendanceDTO.getDate() != null,"att_date", attendanceDTO.getDate());
        return attendanceMapper.selectList(queryWrapper);
    }

    /**
     * 多条件查询部门考勤信息
     */
    public List<AttdDeptVO> getByDeptId(AttendanceDTO attendanceDTO) {
        List<AttdDeptVO> attdDeptVOList = attendanceMapper.selectByDept(attendanceDTO);
        return attdDeptVOList;
    }

    /**
     * 查询全部考勤信息
     */
    public List<AttdDeptVO> getList(AttendanceDTO attendanceDTO) {
        List<AttdDeptVO> attdDeptVOList = attendanceMapper.selectAll(attendanceDTO);
        return attdDeptVOList;
    }

    /**
     * 员工签到
     */
    @Transactional
    public void checkIn(CheckDTO checkDTO) {
        // 查看员工是否存在
        Emp emp = empMapper.selectById(checkDTO.getEmpId());
        if (emp == null) {
            throw new EmployeeNotExistException(MessageConstant.EMPLOYEE_NOT_EXIST);
        }
        Attendance attendance = new Attendance().builder()
                .empId(checkDTO.getEmpId())
                .deptId(emp.getDeptId())
                .attDate(LocalDate.now())
                .checkIn(LocalDateTime.now())
                .build();
        // 假定 9：00 为上班时间，判断是否迟到
        // 获取今天上午9点的时间
        LocalDateTime workStartTime = LocalDateTime.of(
                LocalDate.from(attendance.getCheckIn()),  // 使用签到日期作为当天日期
                LocalTime.of(9, 0)        // 设置时间为上午9点
        );
        // 比较签到时间与上午9点
        boolean isLate = attendance.getCheckIn().isAfter(workStartTime);
        if (isLate) {
            attendance.setStatus("迟到");
        } else {
            attendance.setStatus("正常");
        }
        attendanceMapper.insert(attendance);
    }

    @Override
    public void checkOut(CheckDTO checkDTO) {
        // 查看员工是否存在
        Emp emp = empMapper.selectById(checkDTO.getEmpId());
        if (emp == null) {
            throw new EmployeeNotExistException(MessageConstant.EMPLOYEE_NOT_EXIST);
        }
        // 找到当前签到的数据
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", checkDTO.getEmpId())
                .orderByDesc("att_date");
        List<Attendance> attendanceList = attendanceMapper.selectList(queryWrapper);
        Attendance attendance = attendanceList.get(0);
        attendance.setCheckOut(LocalDateTime.now());
        // 假设 18：00 下班
        LocalDateTime workStartTime = LocalDateTime.of(
                LocalDate.from(attendance.getCheckIn()),  // 使用签到日期作为当天日期
                LocalTime.of(18, 0)        // 设置时间为 18：00
        );
        // 比较签到时间与下午6点
        boolean isLate = attendance.getCheckIn().isBefore(workStartTime);
        if (isLate) {
            attendance.setStatus("早退");
        }
        attendanceMapper.updateById(attendance);
    }
}




